using System;
using System.Collections;
using System.Reflection;
using DataBroker.Dao;

namespace NorthwindDAO
{
	public class Categories : Entity
	{
		private Int32 _CategoryID;
		private String _CategoryName;
		private String _Description;
		private Byte[] _Picture;
	
		///Default constructor
		public Categories ()
		{
		
		}

        /// <summary>
        /// Constructor for joins. 
		/// Available CHILD tables to join with:
		///    - Products ( Products_fk_products_categories )
        /// </summary>
        public Categories(bool Flat, params Entity[] entities)
            : this()
		{
			foreach(Entity entity in entities)
			{
				string[] entityParts = entity.GetType().ToString().Split('.');
				string entityName = entityParts[entityParts.Length - 1];
				PropertyInfo pf = GetType().GetProperty((entityName));
				if (pf != null)
				{
					pf.SetValue(this,new EntityCollection(entity), null);
				}
                entityName = entityName + "_"; ;
                pf = GetType().GetProperty((entityName));
                if (pf != null)
                {
                    pf.SetValue(this, entity, null);
                }
			}
			SetFlat(Flat);
		}
        public Categories(params Entity[] entities)
            : this(true, entities)
		{
		}
	
		[IsNotNullable]
		[IsPK]
		[IsIdentity]
		[SequenceName("Categories")]
		public Int32 Categoryid
		{
			get
			{
				return _CategoryID;
			}
		}
		[IsNotNullable]
		[MaxLength(15)]
		public String Categoryname
		{
			get
			{
				return _CategoryName;
			}
			set
			{
				_CategoryName = value;
			}
		}
		[MaxLength(16)]
		public String Description
		{
			get
			{
				return _Description;
			}
			set
			{
				_Description = value;
			}
		}
		public Byte[] Picture
		{
			get
			{
				return _Picture;
			}
			set
			{
				_Picture = value;
			}
		}
	
        #region Products 
        private EntityCollection _Products;
        [NotInTable]
        [TypeContained(typeof(Products),"Products")]
        public EntityCollection Products
        {
            get
            {
                return _Products;
            }

            set
            {
                _Products = value;
            }
        }
        private Products _Products_;
        [NotInTable]
        public Products Products_
        {
            get
            {
                return _Products_;
            }

            set
            {
                _Products_ = value;
            }
        }
        #endregion
        public override DBJoin GetJoin(string tableName)
        {
			if (string.Compare(tableName, "Products", true) == 0)
			{
				ArrayList left = new ArrayList();
				ArrayList right = new ArrayList();
				left.Add(tableName + ".Categoryid");
				right.Add(GetType().Name + ".Categoryid");
				return new DBJoin(left, right, JoinTypeEnumeration.LEFT_OUTER);
			}
			return null;
		}
	}
}


